from django.db import models

# Create your models here.
from base_model import BaseModel
from user.models import User


class ThreeAddress(BaseModel):
    """三级联动,不允许删除"""
    name = models.CharField(max_length=500, verbose_name='名称')
    parent = models.ForeignKey('self', related_name='lower', null=True, blank=True, verbose_name='上一级目录',
                               on_delete=models.SET_NULL)

    class Meta:
        db_table = 'three_address'
        verbose_name = '省市区三级'
        verbose_name_plural = verbose_name


class Address(BaseModel):
    title = models.CharField(max_length=50, verbose_name='标题')
    place = models.CharField(max_length=500, verbose_name='地址')
    province = models.ForeignKey(ThreeAddress, on_delete=models.PROTECT, related_name='province_addresses',
                                 verbose_name='省')
    city = models.ForeignKey(ThreeAddress, on_delete=models.PROTECT, related_name='city_addresses', verbose_name='市')
    district = models.ForeignKey(ThreeAddress, on_delete=models.PROTECT, related_name='district_addresses',
                                 verbose_name='区')
    phone = models.CharField(max_length=11, verbose_name='收货人手机号')
    name = models.CharField(max_length=20, verbose_name='收货人姓名')
    user = models.ForeignKey(User, related_name='address', db_index=True, on_delete=models.CASCADE, verbose_name='关联用户')

    class Meta:
        db_table = 'address'
        verbose_name = '用户地址'
        verbose_name_plural = verbose_name
